package org.goldfish.spring_tutorial.controller;

import org.goldfish.spring_tutorial.db.WebVisitorActionRepository;
import org.goldfish.spring_tutorial.entity.WebVisitorAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/submit")
public class WebVisitorActionReceiver {

    @Autowired
    private WebVisitorActionRepository webVisitorActionRepository;

    @GetMapping
    public String insertPrepare(
            @RequestParam(value = "userID", defaultValue = "0")
            Long userID,
            Model model) {
        model.addAttribute("userID", userID);
        return "insertForm";
    }

    @PostMapping("/insert")
    public String insert(
            @RequestParam(value = "userID", defaultValue = "0")
            Long userID,
            @RequestParam(value = "type", defaultValue = "Unknown")
            String type,
            Model model
    ) {
        WebVisitorAction action = new WebVisitorAction();
        action.setUserID(userID);
        action.setSiteType(type);
        webVisitorActionRepository.save(action);
        model.addAttribute("action", action);
        return "inserted";
    }

}
